<?php

class Msg
{
    public $code;
    public $msg;
    public $data;
    public static $MESSAGE=[
        -2=>"permission not allowed",
        -1=>"login fail",
        0=>"ok",
        1=>"add fail",
        2=>"delete fail",
        3=>"edit fail",
        4=>"get fail"
    ];
    public function __construct($code=0,$msg="ok",$data=[])
    {
        $this->code=$code;
        $this->msg=$msg;
        $this->data=$data;
    }

    public function __toString()
    {
        // TODO: Implement __toString() method.
        $message = json_encode(
            $this
        );
        if (empty($this->data)){
            $message = str_replace("[]","{}",$message);
        }
        return $message;
    }
    public function setCode($code){
        $this->code = $code;
        $this->msg = self::$MESSAGE[$code];
    }

}